package com.topcheer.common.base.web;

import com.github.pagehelper.PageInfo;
import com.topcheer.common.base.service.impl.BaseBiz;
import com.topcheer.common.base.vo.BaseResponse;
import com.topcheer.common.base.vo.PageVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author xutao
 * @Package com.topcheer.common.base.web
 * @Description: ${todo}
 * @date 2020/3/29 20:16
 */
@Slf4j
@RestController
public class BaseController<Biz extends BaseBiz, T> {

    @Autowired
    protected Biz biz;

    @PostMapping("")
    public BaseResponse<T> insert(@RequestBody T entity) {
        biz.insert(entity);
        return new BaseResponse<T>().code(HttpStatus.OK.value()).msg(HttpStatus.OK.getReasonPhrase());
    }

    @GetMapping("/{id}")
    public BaseResponse<T> selectOne(@PathVariable("id") Long id) {
        Object o = biz.selectOne(id);
        return new BaseResponse<T>().data((T)o);
    }

    @GetMapping("/page")
    public BaseResponse<PageInfo> page(PageVo vo, T entity) throws Exception {
        PageInfo pageInfo = biz.selectPage(vo, entity);
        return new BaseResponse<PageInfo>().data(pageInfo);
    }

    @PutMapping("")
    public BaseResponse<T> update(@RequestBody T entity) {
        biz.update(entity);
        return new BaseResponse<T>();
    }

    @DeleteMapping("/{id}")
    public BaseResponse<T> delete(@PathVariable("id") Long id) {
        biz.deleteById(id);
        return new BaseResponse<T>();
    }

    @GetMapping("/all")
    public BaseResponse<List> selectAll() {
        List list = biz.selectAll();
        return new BaseResponse<List>().data(list);
    }

}
